﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace BandwidthMonitor
{
    public partial class frmMain : Form
    {
        private Program m_Owner;

        public frmMain(Program owner)
        {
            InitializeComponent();

            m_Owner = owner;

            WindowHelper window = new WindowHelper();
            window.PositionFormRelativeToTaskbar(this);
        }

        private void frmMain_Load(object sender, EventArgs e)
        {
            lblTelephone.Text = m_Owner.m_Telephone + ", last usage update: " + m_Owner.m_LastUpdate;
            lblAllowanceUntil.Text = m_Owner.m_broadbandTotalAllowance.ToString() + "GB allowance " + m_Owner.m_broadbandAllowanceUntil;
            lblUsedStatus.Text = m_Owner.m_broadbandTotalUsed.ToString() + "GB used, Status: ";

            switch (m_Owner.CurrentStatus())
            {
                case Program.UsageStatus.unknown:
                    lblUsedStatus.Text += "Unknown";
                    break;
                case Program.UsageStatus.green:
                    lblUsedStatus.Text += "OK";
                    break;
                case Program.UsageStatus.amber:
                    lblUsedStatus.Text += "Warning";
                    break;
                case Program.UsageStatus.red:
                    lblUsedStatus.Text += "Danger";
                    break;
            };

            uiStatus.Threshold1.Value = Properties.Settings.Default.AmberThreshold;
            uiStatus.Threshold2.Value = Properties.Settings.Default.RedThreshold;
            uiStatus.Value = m_Owner.PercentageUsed;

            uiMonitor.Value = 0;

            for (byte i = 0; i <= 2; i++)
            {
                uiMonitor.Range_Idx = i;
                uiMonitor.RangeEnabled = false;
                uiMonitor.RangeEndValue = 0;
            }

            if (m_Owner.PercentageUsed < Properties.Settings.Default.AmberThreshold) uiMonitor.Range_Idx = 0;
            if (m_Owner.PercentageUsed >= Properties.Settings.Default.AmberThreshold && m_Owner.PercentageUsed < Properties.Settings.Default.RedThreshold) uiMonitor.Range_Idx = 1;
            if (m_Owner.PercentageUsed >= Properties.Settings.Default.RedThreshold) uiMonitor.Range_Idx = 2;

            tmrAnimate.Enabled = true;
        }

        /// <summary>
        /// Occurs when the size has changed for the form.
        /// We'll check to see if the form has minimized.  If so, close it.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void frmMain_SizeChanged(object sender, System.EventArgs e)
        {
            if (this.WindowState == FormWindowState.Minimized)
            {
                this.Close();
            }
        }

        // Cancels the resize cursor
        protected override void WndProc(ref Message message)
        {
            const int WM_NCHITTEST = 0x0084;
            switch (message.Msg)
            {
                case WM_NCHITTEST:
                    return;
            }
            base.WndProc(ref message);
        }

        private void tmrAnimate_Tick(object sender, EventArgs e)
        {
            uiMonitor.Value += 0.5f;
            
            //uiMonitor.Range_Idx = 0;
            uiMonitor.RangeEnabled = true;
            uiMonitor.RangeEndValue = uiMonitor.Value;

            if ((uiMonitor.Value += 0.5f) > m_Owner.m_broadbandTotalUsed)
            {
                uiMonitor.Value = m_Owner.m_broadbandTotalUsed;
                tmrAnimate.Enabled = false;
            }
        }
    }
}
